﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace YKXiangQi.Enums
{
    public class EnumHelper
    {
        public static IList<EnumMapper<T>> LoadEnums<T>()
        {
            var result = new List<EnumMapper<T>>();
            var enums = Enum.GetValues(typeof (T));

            foreach (var @enum in enums)
            {
                var enumType = typeof (T);
                var field = enumType.GetField(@enum.ToString());
                var attribute = Attribute.GetCustomAttribute(field, typeof (DescriptionAttribute)) as DescriptionAttribute;

                result.Add(attribute != null
                               ? new EnumMapper<T>((T) @enum, attribute.Description)
                               : new EnumMapper<T>((T) @enum, field.Name));
            }

            return result;
        }
    }
}
